import redis
class RedisConn:
    def __init__(self) -> None:
        self.r = redis.Redis()

    # 字符串添加
    def str_add(self,key,value,time=0):
        if time == 0:
            self.r.set(key,value)
        else:
            self.r.setex(key,time,value)

    # 字符串获取
    def str_get(self,key):
        value = self.r.get(key)
        if value:
            value = value.decode()
        return value

    def str_del(self,key):
        return self.r.delete(key)

    # hash类型封装
    # 添加
    def hash_add(self,key,params,value):
        self.r.hset(key,params,value)
    # 获取所有
    def getall_hash(self,key):
        return self.r.hgetall(key)
    # 获取单个
    def getone_hash(self,key,params):
        return self.r.hget(key,params)
    # 模糊查询
    def getvalues(self,key):
        list = []
        values = self.r.keys(key)
        if values:
            for i in values:
                list.append(i.decode())
        return list
    # 删除
    def del_hash(self,key,params):
        return self.r.hdel(key,params)

r = RedisConn()

userid = 1
goods = 3
# key = "col" + str(userid) + "pro" + str(goods)
# r.hash_add(key,"id",goods)
# r.hash_add(key,"name","商品"+str(goods))


# findkey = "col" + str(userid) + "*"
# gidlist = r.getvalues(findkey)
# reslist = []
# for i in gidlist:
#     goods = r.getall_hash(i)
#     dict = {}
#     for k,v in goods.items():
#         kk = k.decode()
#         vv = v.decode()
#         dict[kk] = vv
#     reslist.append(dict)
# print(reslist)